import { query, queryAll } from '../utils/query';
import { getSiblings } from '../utils/dom';
import type { CascaderOptions } from '../types/options';
import type { SearchResultItem } from '../utils/data';

/**
 * 选择管理器
 * 负责级联选择器的选项选择逻辑
 */
export class SelectionManager {
    private uuid: string;
    private options: CascaderOptions;

    constructor(uuid: string, options: CascaderOptions) {
        this.uuid = uuid;
        this.options = options;
    }

    /**
     * 选择菜单项
     * @param currentItem 当前选择的菜单项
     */
    public selectItem(currentItem: HTMLElement) {
        const currentLevel = parseInt(currentItem.dataset.parentLevel ?? "0");

        const siblings = getSiblings(currentItem);
        for (let index = 0; index < siblings.length; index++) {
            const element = siblings[index] as HTMLElement;
            element.classList.remove("cascader-container_menus_menu_item_active");
        }
        currentItem.classList.add("cascader-container_menus_menu_item_active");

        const allMenu = queryAll(".cascader-container_menus_menu", this.uuid) ?? [];
        for (let index = 0; index < allMenu.length; index++) {
            const element = allMenu[index] as HTMLElement;
            const level = parseInt(element.dataset.level ?? "0");

            if (level > currentLevel) {
                query(".cascader-container_menus", this.uuid)?.removeChild(element);
            }
        }
    }

    /**
     * 选择搜索结果项
     * @param resultItem 搜索结果项
     * @param isMultiple 是否为多选模式
     */
    public selectSearchResult(resultItem: HTMLElement, isMultiple: boolean = false): void {
        const { mode = 'single' } = this.options;
        
        // 多选模式下的处理
        if (isMultiple || mode === 'multiple') {
            // 切换选中状态
            if (resultItem.classList.contains('cascader-container_search_result_item_active')) {
                resultItem.classList.remove('cascader-container_search_result_item_active');
            } else {
                resultItem.classList.add('cascader-container_search_result_item_active');
            }
        } else {
            // 单选模式下的处理
            // 移除所有其他搜索结果的选中状态
            const allSearchResults = queryAll('.cascader-container_search_result_item', this.uuid) || [];
            allSearchResults.forEach((item) => {
                item.classList.remove('cascader-container_search_result_item_active');
            });
            // 选中当前项
            resultItem.classList.add('cascader-container_search_result_item_active');
        }
    }

    /**
     * 检查搜索结果项是否已选中
     * @param resultItem 搜索结果项
     * @returns 是否已选中
     */
    public isSearchResultSelected(resultItem: HTMLElement): boolean {
        return resultItem.classList.contains('cascader-container_search_result_item_active');
    }

    /**
     * 获取所有已选中的搜索结果项
     * @returns 已选中的搜索结果项数组
     */
    public getSelectedSearchResults(): HTMLElement[] {
        const allSearchResults = queryAll('.cascader-container_search_result_item_active', this.uuid) || [];
        return Array.from(allSearchResults) as HTMLElement[];
    }
}